package com.zxhwolfe.androidparsertest;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

public class AndroidParserTest extends Activity {
	static String domString = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		xmlSAXTest();// sax
		// xmlDOMTest();//dom
	}

	private void xmlSAXTest() {
		/* Create a new TextView to display the parsingresult later. */
		EditText tv = new EditText(this);
		try {
			/* Create a URL we want to load some xml-data from. */
			URL url = new URL("http://wap.baidu.com");
			/* Get a SAXParser from the SAXPArserFactory. */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();

			/* Get the XMLReader of the SAXParser we created. */
			XMLReader xr = sp.getXMLReader();
			/* Create a new ContentHandler and apply it to the XML-Reader */
			ExampleHandler myExampleHandler = new ExampleHandler();
			xr.setContentHandler(myExampleHandler);

			/* Parse the xml-data from our URL. */
			xr.parse(new InputSource(url.openStream()));
			/* Parsing has finished. */
			tv.setText(ExampleHandler.reportData);
		} catch (Exception e) {
			/* Display any Error to the GUI. */
			tv.setText("Error: " + e.getMessage());
			Log.e("xmlSAXTest", "WeatherQueryError", e);
		}
		/* Display the TextView. */
		this.setContentView(tv);
	}

	private void xmlDOMTest() {
		EditText tv = new EditText(this);
		try {
			URL url = new URL("http://wap.baidu.com");
			DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();
			Document doc = dombuilder.parse(new InputSource(url.openStream()));
			// doc.normalize();
			Element root = doc.getDocumentElement();
			NodeList books = root.getChildNodes();
			process(books);
		} catch (Exception e) {
			tv.setText("Error: " + e.getMessage());
			Log.e("xmlDOMTest", "WeatherQueryError", e);
		}
		domString += "over";
		tv.setText(domString);
		/* Display the TextView. */
		this.setContentView(tv);
	}

	public static void process(NodeList nodelist) {
		try {
			for (int i = 0; i < nodelist.getLength(); i++) {
				Node book = nodelist.item(i);
				if (book.hasChildNodes()) {
					domString += book.getNodeName() + "\n";
					if (book.hasAttributes()) {
						for (int k = 0; k < book.getAttributes().getLength(); k++) {
							domString += book.getAttributes().item(k).getNodeName() + " = " + book.getAttributes().item(k).getNodeValue() + "\n";
						}
					}
					NodeList booklistList = book.getChildNodes();
					process(booklistList);
				} else {
					if (null == book.getNodeValue() || "".equals(book.getNodeValue().trim())) {
						if (!"#text".equals(book.getNodeName())) {
							domString += book.getNodeName() + "\n";
						}
					} else {
						// domString +=
						// book.getNodeName()+" - "+book.getNodeValue()+"\n";
						domString += book.getNodeValue() + "\n";
					}
					if (book.hasAttributes()) {
						for (int k = 0; k < book.getAttributes().getLength(); k++) {
							domString += book.getAttributes().item(k).getNodeName() + " = " + book.getAttributes().item(k).getNodeValue() + "\n";
						}
					}
				}
			}

		} catch (Exception e) {
			Log.e("xmlDOMTest", "WeatherQueryError", e);
		}
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
	}
}